import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator {
    private JFrame frame;
    private JTextField inputField;
    private JTextField resultField;
    private StringBuilder inputBuffer;

    public Calculator() {
        frame = new JFrame("简易计算器");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 400);  // 调整窗口高度
        frame.setLayout(new BorderLayout());

        inputField = new JTextField();
        resultField = new JTextField();
        resultField.setEditable(false);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2, 1));
        panel.add(inputField);
        panel.add(resultField);

        frame.add(panel, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(5, 4));  // 增加一行按钮，以便添加更多功能

        // 数字按钮
        for (int i = 1; i <= 9; i++) {
            addButton(buttonPanel, String.valueOf(i));
        }
        addButton(buttonPanel, "0");

        // 运算符按钮
        addButton(buttonPanel, "+");
        addButton(buttonPanel, "-");
        addButton(buttonPanel, "*");
        addButton(buttonPanel, "/");
        addButton(buttonPanel, ".");  // 添加小数点按钮

        // 清除和计算按钮
        addButton(buttonPanel, "C");
        addButton(buttonPanel, "=");

        frame.add(buttonPanel, BorderLayout.CENTER);

        inputBuffer = new StringBuilder();

        frame.setVisible(true);
    }

    private void addButton(Container container, String text) {
        JButton button = new JButton(text);
        button.addActionListener(new ButtonClickListener());
        container.add(button);
    }

    private class ButtonClickListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String buttonText = ((JButton) e.getSource()).getText();

            switch (buttonText) {
                case "=":
                    calculate();
                    break;
                case "C":
                    clear();
                    break;
                default:
                    inputBuffer.append(buttonText);
                    inputField.setText(inputBuffer.toString());
            }
        }

        private void calculate() {
            try {
                ScriptEngineManager manager = new ScriptEngineManager();
                ScriptEngine engine = manager.getEngineByName("JavaScript");
                Object result = engine.eval(inputBuffer.toString());

                // 将结果显示在结果文本框中
                resultField.setText(result.toString());

                // 清空输入缓冲区
                inputBuffer.setLength(0);
            } catch (ScriptException ex) {
                JOptionPane.showMessageDialog(frame, "计算错误，请检查输入的表达式！", "错误", JOptionPane.ERROR_MESSAGE);
                ex.printStackTrace();  // 输出异常信息到控制台
            }
        }

        private void clear() {
            // 清空输入缓冲区和结果文本框
            inputBuffer.setLength(0);
            inputField.setText("");
            resultField.setText("");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Calculator();
            }
        });
    }
}
